﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UFPC.Models;

namespace UFPC.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class OrderController : Controller
    {
        private IUfpcRepository _repository;

        public OrderController() : this(new UfpcRepository())
        { 
            
        }

        public OrderController(IUfpcRepository repository)
        {
            _repository = repository;
        }
        
        //
        // GET: /Order/

        public ActionResult Index()
        {
            return View(_repository.ListOrders());
        }

        //
        // GET: /Order/Details/5

        public ActionResult Details(int id)
        {
            return View(_repository.OrderSummary(id));
        }

        public ViewResult Add()
        {
            ViewData["ProductList"] = new SelectList(_repository.ListProducts(), "ProductId", "Name");
            
            return View("OrderDetailEditorRow", new OrderDetail());
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            Order order = new Order();
            order.OrderDate = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"));
            
            ViewData["CustomerList"] = new SelectList(_repository.ListCustomers(), "CustomerId","Name");

            return View(order);
        } 

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude="OrderId,OrderDetailId")]Order newOrder, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                _repository.AddOrder(newOrder);
                _repository.Save();

                return RedirectToAction("Index");
            }

            return View();
        }
        
        //
        // GET: /Order/Edit/5
 
        public ActionResult Edit(int id)
        {
            ViewData["CustomerList"] = new SelectList(_repository.ListCustomers(), "CustomerId", "Name");
            ViewData["ProductList"] = new SelectList(_repository.ListProducts(), "ProductId", "Name");
            
            return View(_repository.OrderSummary(id));
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit([Bind(Exclude="OrderId,OrderDetailId")]int id, FormCollection collection)
        {
            Order order = _repository.OrderSummary(id);
            
            TryUpdateModel(order);
            _repository.Save();

            return RedirectToAction("Index");
        }

        //
        // GET: /Order/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_repository.OrderSummary(id));
        }

        //
        // POST: /Order/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Order order = _repository.OrderSummary(id);
            IEnumerable<OrderDetail> orderDetails = _repository.OrderDetails(id);

            _repository.DeleteOrder(order, orderDetails);
            _repository.Save();

            return RedirectToAction("Index");
        }
    }
}
